<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Service as ServiceModel;

class ServiceService
{
    /**
     * 上门服务列表统计
     * @param $where
     * @return mixed
     */
    public static function countService($where = [])
    {
        return ServiceModel::countService($where);
    }

    /**
     * 上门服务列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return mixed
     */
    public static function getServiceList($where, $page, $pagesize)
    {
        $list = ServiceModel::getServiceList($where, $page, $pagesize);
        foreach ($list as $k => $v) {
            $list[$k]['ids'] = $v['id'] . '<br><span class="color3">' . $v['uid'] . '</span>';
            $headimg = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $v['headimg'] . '" \' + \'alt=""></div>';
            $list[$k]['nickname_data'] = '<div class="d-fix1 mar-lefts">' . $headimg . '<div><p class="datas">' . emojiDecode($v['nickname']) . '</p>';
            // 省市区
            $list[$k]['province'] = LocationService::getLocationValue(['location_id' => $v['province']]);
            $list[$k]['city'] = LocationService::getLocationValue(['location_id' => $v['city']]);
            $list[$k]['area'] = LocationService::getLocationValue(['location_id' => $v['area']]);
            $address = $list[$k]['province'] . $list[$k]['city'] . $list[$k]['area'] . $v['location'];

            $list[$k]['mobile_data'] = $v['name'] . '<br><span class="color3">' . $v['mobile'] . '</span>';

            $list[$k]['company_data'] = $v['company'] . '<br><span class="color3">' . $address . '</span>';

            $list[$k]['service_data'] = $v['service_number'] . '<br><span class="color3">' . $v['resource_number'] . '</span>';

            $home_time = date('Y-m-d H:i:s', $v['home_time']);
            $service_time = $v['service_time'] ? date('Y-m-d H:i:s', $v['service_time']) : '-';
            $end_time = $v['end_time'] ? date('Y-m-d H:i:s', $v['end_time']) : '-';
            $list[$k]['create_data'] = '<p class="color1">' . $home_time . '</p><p class="color2">' . $service_time . '</p><p>' . $end_time . '</p>';


        }

        return $list;

    }

    /**
     * 上门服务列表统计
     * @param $where
     * @return mixed
     */
    public static function countServiceList($where)
    {
        return ServiceModel::countServiceList($where);
    }

    /**
     * 单上门服务
     * @param $where
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findService($where)
    {
        return ServiceModel::findService($where);
    }

    /**
     * 更新上门服务
     * @param $data
     * @param $where
     * @return ServiceModel
     */
    public static function updateService($data, $where)
    {
        return ServiceModel::updateService($data, $where);
    }

    /**
     * 服务地区分布
     * @return mixed
     */
    public static function getServiceArea()
    {
        $data = ServiceModel::getServiceArea();
        $datas = getAreaData($data);
        return ['count' => count($data), 'datas' => $datas];
    }
}